package com.zbf.web;

import com.zbf.entity.MyUploadEntity;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/7  19:13
 * 描述:
 */
@RestController
public class TestWebController {

    @Autowired
    private MinioClient minioClient;

    /**
     * 作者: LCG
     * 日期: 2021/6/7  20:41
     * 描述: 上传图片并获取访问路径
     * @Param [myUploadEntity]
     * @Return java.util.List<java.lang.String>
     */
    @RequestMapping("uploadFile")
    public List<String> uploadFile(MyUploadEntity myUploadEntity) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, InvalidExpiresRangeException {

        MultipartFile[] file = myUploadEntity.getFile();

        List<String> list=new ArrayList<String>();

        for (MultipartFile multipartFile : file) {
            PutObjectOptions putObjectOptions=new PutObjectOptions(multipartFile.getSize(),-1);
            //设置文件的内容类型
            putObjectOptions.setContentType(multipartFile.getContentType());

            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(multipartFile.getBytes());

            minioClient.putObject("images",multipartFile.getOriginalFilename(),byteArrayInputStream,putObjectOptions);
            //String imageUrl = minioClient.getObjectUrl("images",multipartFile.getOriginalFilename());
            String images1 = minioClient.presignedGetObject("images", multipartFile.getOriginalFilename(), 50000);
            list.add(images1);
        }


        return list;
    }

    @RequestMapping("getImage")
    public void getImage(Map<String,String> map, HttpServletResponse response) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, InvalidExpiresRangeException {
        InputStream images = minioClient.getObject("images", "28.jpg");
        byte[] bytes=new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        int rc = 0;
        while((rc=images.read(bytes))>0){
            byteArrayOutputStream.write(bytes,0,rc);
        }
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-Disposition", "attachment;filename="+new String("好的图片.jpg".getBytes(),"ISO-8859-1"));
        response.getOutputStream().write(byteArrayOutputStream.toByteArray());

    }

}
